﻿// See https://aka.ms/new-console-template for more information
using Azure;
using Azure.AI.Inference;
using Microsoft.Extensions.AI;
using System.Text;

Console.WriteLine("Hello, World!");

//创建访问ai终结点
//Uri uri = new("https://models.inference.ai.azure.com");
//AzureKeyCredential azureKeyCredential = new AzureKeyCredential("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");

//IChatClient chatClient = new ChatCompletionsClient(uri, azureKeyCredential).AsIChatClient("DeepSeek-V3-0324");

Uri endPoint = new("http://localhost:11434");
IChatClient chatClient = new OllamaChatClient(endPoint, "qwen2:7b");

List<ChatMessage> messages = new List<ChatMessage>();
//while (true)
//{
//    Console.Write("请输入你的问题：");
//    string msg=Console.ReadLine();
//    messages.Add(new ChatMessage(Microsoft.Extensions.AI.ChatRole.User, msg));

//    var response=await chatClient.GetResponseAsync(messages);

//    messages.Add(new ChatMessage(Microsoft.Extensions.AI.ChatRole.Assistant, response.Text));

//    Console.WriteLine(response.Text);
//}

while (true)
{
    Console.Write("请输入你的问题：");
    string msg = Console.ReadLine();
    messages.Add(new ChatMessage(Microsoft.Extensions.AI.ChatRole.User, msg));

    Console.Write("人工智能回答：");
    var response =  chatClient.GetStreamingResponseAsync(messages);

   
    StringBuilder stringBuilder = new StringBuilder();
    await foreach (var item in response)
    {
        stringBuilder.AppendLine(item.Text);
        Console.Write(item.Text);
    }
    Console.WriteLine();
    messages.Add(new ChatMessage(Microsoft.Extensions.AI.ChatRole.Assistant, stringBuilder.ToString()));

    
}